'use strict';

angular.module('app')
    .controller('maintainController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster','$modal',
        function($scope, $rootScope, $state, $timeout, toaster,$modal) {
            $scope.title = '设备报障列表';
            $scope.param = { };
            $scope.loading = false;

            $scope.search = function () {
                $scope.loading = true;
                if($scope.pageFlag != 1){//未点击翻页
                    $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
                }else{//点击翻页
                    $scope.pageFlag = 2;//将开关值设置为2
                }
                $.ajax({//页面初始化数据请求
                    type: 'POST',
                    dataType: 'json',
                    contentType:'application/json;charset=UTF-8',
                    url : '/dac/dm/DmMaintain/list',
                    data: angular.toJson($scope.param)
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        console.log(result);
                        for(var i=0;i<result.data.length;i++){
                            if(result.data[i].status=="1"){
                                result.data[i].status='待派单'
                            }
                            if(result.data[i].status=="2"){
                                result.data[i].status='待处理'
                            }
                            if(result.data[i].status=="3"){
                                result.data[i].status='待确认'
                            }
                            if(result.data[i].status=="4"){
                                result.data[i].status='已完成'
                            }
                        }
                        $scope.pageInfo = result;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply(function(){
                        $("#allCheck").prop("checked",false); 
                    })
                });

            }
            $scope.search();

            $scope.clearSearch = function() {
                $scope.param.keyword= null;
                $scope.search();
            }

            $scope.disableItem = function(id, enable) {

            }

            // 翻页
            $scope.pagination = function (page) {
                $scope.param.pageNum=page;
                $scope.pageFlag = 1;//将开关值设置为1
                $scope.search();
            };
            //从本地获取localstorage数据，对权限按钮做隐藏
            var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
            personAuth = personAuth.split(',');
            for(var i = 0;i<personAuth.length;i++){
                if(personAuth[i]=='dm.deviceCategory.read'){
                    $scope.read = true;
                }
                if(personAuth[i]=='dm.deviceCategory.add'){
                    $scope.add = true;
                }
                if(personAuth[i]=='dm.deviceCategory.update'){
                    $scope.update = true;
                }
                if(personAuth[i]=='dm.deviceCategory.delete'){
                    $scope.dele = true;
                }
            }

            //全选按钮
            $scope.allCheck = function($event){
                var allcheck = $event.target ;//全选按钮
                var checked = allcheck.checked ;//选中状态
                var checkBoxs = $(".tbody input[name='Fruit']");
                checkBoxs.each(function(i){
                    this.checked = checked;
                });
            }
            //检测没有全选就将全选按钮变为未选状态
            $scope.checkStatus = function () {
                var checkBoxs = $(".tbody input[name='Fruit']").length;//单选框的个数
                var checkeds = $(".tbody input[name='Fruit']:checked").length//单选框选中的个数
                $("#allCheck").prop("checked",checkBoxs === checkeds ? true : false);//个数与选中个数相同时，全选按钮选中，否则不选中
            }
            //批量派单模态框
            $scope.allMaintain = function () {
                var checkBoxs = $('.tbody').find('input[type=checkbox]');
                var statusContro = $('.tbody').find('td.statusContro');
                var idArray = [];//存放id数组
                var statusArray = [];
                checkBoxs.each(function(i){
                    if( this.checked == true){
                        idArray.push(this.value);//遍历表格的单选框，如果选中，就获取他value里存的id
                        statusArray.push(statusContro[i].innerText)//statusContro 的下标与checkedBoxs一一对应，所以可以根据 i 获取选中派单的状态值
                    }
                });
                console.log(statusArray)
                if(idArray.length<=0){//如果未选中任何设备，此处批量派单将不能执行
                    toaster.clear('*');
                    toaster.pop('error', '', '至少选择一个派单的设备！');
                    return
                }else{
                    console.log(idArray)
                    var modalInstance = $modal.open({
                        templateUrl : 'groupList.html',// script标签中定义的id
                        controller : 'appUserModalCtrl',// modal对应的Controller
                        size: 'lg',
                        resolve : {
                            items : function() {
                                return idArray;
                            }
                        }
                    });
                    modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

                    });
                    modalInstance.result.then(function(result) {

                    }, function(reason) {
                        console.log(reason);
                    });

                }

            }
            // 单个派单模态框
            $scope.openDmBuilding = function(maintainId,status) {
                    var idArray=[];//以数组形式存放传给后台
                    idArray.push(maintainId)
                    var modalInstance = $modal.open({
                        templateUrl : 'groupList.html',// script标签中定义的id
                        controller : 'appUserModalCtrl',// modal对应的Controller
                        size: 'lg',
                        resolve : {
                            items : function() {
                                return idArray;
                            }
                        }
                    });

                    modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数

                    });
                    modalInstance.result.then(function(result) {

                    }, function(reason) {
                        console.log(reason)
                    });
                };
            // 派单控制器
            app.controller('appUserModalCtrl', function($scope, $rootScope, $modal,$modalInstance,items) {
                var maintainId=items;//定义全局设备存放获取到选择的设备的id，供给下面派单使用
                console.log(items);
                $scope.param = {
                    idArray: items
                }
                console.log($scope.param)
                $scope.search = function () {
                    $scope.loading = true;
                    if($scope.pageFlag != 1){//未点击翻页
                        $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
                    }else{//点击翻页
                        $scope.pageFlag = 2;//将开关值设置为2
                    }
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : '/dac/dm/DmMaintain/groupList',
                        data: angular.toJson($scope.param)
                    }).then(function(result) {
                        $scope.loading = false;
                        if (result.httpCode == 200) {
                            $scope.pageInfo = result;
                            console.log(result.data)
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                        $scope.$apply();
                    });
                }
                $scope.search();
                //单个派单
                $scope.selectAppUser = function(userId) {
                    $scope.param = { };
                    $scope.param.userId = userId;//选择人员id
                    $scope.param.maintainId = maintainId;//上面定义的全局变量设备id list
                    console.log($scope.param)
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : '/dac/dm/DmMaintain/dispatch',
                        data: angular.toJson($scope.param)
                    }).then(function(result) {
                        $scope.loading = false;
                        $modalInstance.close();
                        if (result.httpCode == 200) {
                            $scope.pageInfo = result;
                            toaster.clear('*');
                            toaster.pop('success', '', "派单成功");
                            $timeout(function(){
                                $state.go('main.dm.DmMaintain.list',null,{
                                    reload:true
                                });
                            },1000);
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                        $scope.$apply();
                    });
                };
                
                $scope.selectGroup = function(groupCode) {
                    $scope.param = { };
                    $scope.param.groupCode = groupCode;//选择人员id
                    $scope.param.maintainId = maintainId;//上面定义的全局变量设备id list
                    console.log($scope.param)
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        contentType:'application/json;charset=UTF-8',
                        url : '/dac/dm/DmMaintain/dispatch',
                        data: angular.toJson($scope.param)
                    }).then(function(result) {
                        $scope.loading = false;
                        $modalInstance.close();
                        if (result.httpCode == 200) {
                            $scope.pageInfo = result;
                            toaster.clear('*');
                            toaster.pop('success', '', "派单成功");
                            $timeout(function(){
                                $state.go('main.dm.DmMaintain.list',null,{
                                    reload:true
                                });
                            },1000);
                        } else {
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                        }
                        $scope.$apply();
                    });
                };

                $scope.cancel = function() {
                    $modalInstance.dismiss('cancel');
                };

                // 翻页
                $scope.pagination = function (page) {
                    $scope.param.pageNum=page;
                    $scope.pageFlag = 1;//将开关值设置为1
                    $scope.search();
                };
            });

        } ]);